/*
 * ULSF Sagittarius IDA
 * Copyright (C) 2012, John Pritchard, all rights reserved.
 * 
 * This program is free software: you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see
 * <http://www.gnu.org/licenses/>.
 */
package sagittarius.design;

import sagittarius.Field;
import sagittarius.Visualization;

import java.io.IOException;

/**
 * The Sears-Haack ideal body for minimal wave drag.
 * 
 * Input parameters have real (double) type, but any fractional parts
 * of these values are ignored.
 */
public abstract class SearsHaack
    extends VolumeHyperbolic
{

    public final static double _3PI2 = 3.0 * Math.pow(Math.PI,2.0);
    public final static double _3PI2_16 = (_3PI2 / 16.0);
    /**
     * Radius and length
     */
    public static abstract class RL
        extends SearsHaack
    {
        public final static double Volume(double radius, double length){
            return (_3PI2_16 * Math.pow(Math.floor(radius),2.0) * Math.floor(length));
        }

        public RL(Visualization data, double radius, double length){
            super(data, Math.floor(radius), Math.floor(length), Volume(radius,length));
        }
        public RL(Visualization data){
            this(data,data.getRadius(),data.getLength());
        }
    }
    /**
     * Volume and length
     */
    public static abstract class VL
        extends SearsHaack
    {
        public final static double Radius(double volume, double length){
            return Math.sqrt(Math.floor(volume) / (_3PI2_16 * Math.floor(length)));
        }

        public VL(Visualization data, double volume, double length){
            super(data,Radius(volume,length),Math.floor(length),Math.floor(volume));
        }
    }
    /**
     * Volume and radius
     */
    public static abstract class VR
        extends SearsHaack
    {
        public final static double Length(double volume, double radius){
            return (Math.floor(volume) / (_3PI2_16 * Math.pow(Math.floor(radius),2.0)));
        }

        public VR(Visualization data, double volume, double radius){
            super(data,Math.floor(radius),Length(volume,radius),Math.floor(volume));
        }
    }


    public final double dw, cdw;


    protected SearsHaack(Visualization data, double radius, double length, double volume){
        super(data,radius,length,volume);

        this.dw = (64 * Math.pow(volume,2.0))/(Math.PI * Math.pow(length,3.0));

        this.cdw = (24 * volume)/Math.pow(length,3.0);
    }



    /**
     * @param x Longitudinal coordinate
     * @return Radius of circular ideal cross section 
     */
    public double radiusForX(double x){
        /*
         * Fixed volume distribution
         * 
         * See NACA TR 1284, Jones 1956, Appendix A
         */
        double t = Math.abs(1-Math.pow(this.centerOfX(x),2));

        double r = Math.sqrt(Math.sqrt(Math.pow(t,3)));

        return (this.radius * r);
    }
}
